import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade
import gobject
import os, gettext
from CVSConstants import *

class CVSLogEntry(object):
  def __init__(self):
    source = gtk.glade.XML(GLADE_FILE, 'commit window', gettext.textdomain())
    self.window = source.get_widget('commit window')
    self.text_view = source.get_widget('commit text')
    self.text_buffer = self.text_view.get_buffer()

    source.signal_autoconnect(self)
    self.window.show_all()
    
  def getLogMsg(self):
    rv = self.window.run()
    if rv == gtk.RESPONSE_OK:
      text = self.text_buffer.get_text(self.text_buffer.get_start_iter(),
                                       self.text_buffer.get_end_iter())
      self.window.destroy()
      return text
    else:
      self.window.destroy()
      raise RuntimeError

class CVSStatus(object):
  def __init__(self, model):
    source = gtk.glade.XML(GLADE_FILE, 'status window', gettext.textdomain())
    self.window = source.get_widget('status window')
    self.progress = source.get_widget('status progress')
    self.text_view = source.get_widget('status text')
    self.text_buffer = self.text_view.get_buffer()
    
    self.text_buffer.create_tag('default', family='Monospace', family_set=True)
    self.window.set_title(model.getAction())
    self.progress.set_text(model.getActivity())
        
    self.model = model
    model.addObserver('cvs-closed', self._onComplete)
    model.addObserver('cvs-waiting', self._onPulse)
    model.addObserver('cvs-started', self._onStart)
    model.addObserver('cvs-message-received', self._appendText)
    model.addObserver('cvs-error-received', self._appendText)

    source.signal_autoconnect(self)
    self.window.show_all()

  def _onStart(self, model, path, cmd):
    self._appendText(None, 'cd %s\n%s' % (path, cmd+'\n'))
    
  def _onComplete(self, model, rv):
    self._appendText(model, 'returned: %d' % rv)
    self.progress.set_text('Complete')
    self.progress.set_fraction(1.0)

  def _onPulse(self, model):
    self.progress.pulse()

  def _appendText(self, model, text):
    end = self.text_buffer.get_end_iter()
    self.text_buffer.insert_with_tags_by_name(end, str(text), 'default')

  def _onSave(self, button):
    dlg =  gtk.FileChooserDialog('Save', self.window,
                                 gtk.FILE_CHOOSER_ACTION_SAVE,
                                 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                  gtk.STOCK_SAVE, gtk.RESPONSE_OK))
    rv = dlg.run()
    if rv == gtk.RESPONSE_OK:
      f = file(dlg.get_filename(), 'w')
      s = self.text_buffer.get_start_iter()
      e = self.text_buffer.get_end_iter()
      text = self.text_buffer.get_text(s, e)
      f.write(text)
      f.close()
    dlg.destroy()

  def _onDestroy(self, window):
    self.model.end()

  def _onClose(self, button):
    self.window.destroy()

class CVSCheck(object):
  def __init__(self, new=False):
    source = gtk.glade.XML(GLADE_FILE, 'check window', gettext.textdomain())
    self.window = source.get_widget('check window')
    self.username = source.get_widget('username')
    self.server = source.get_widget('server')
    self.path = source.get_widget('path')
    self.module = source.get_widget('module')
    self.protocol = source.get_widget('protocol')

    # creating a new module, use known path
    if new:
      self.module.set_sensitive(False)
      self.module.set_text(CVS_PATH)

    source.signal_autoconnect(self)
    self.window.show_all()

  def getRepository(self):
    rv = self.window.run()
    if rv == gtk.RESPONSE_OK:
      username = self.username.get_text()
      server = self.server.get_text()
      path = self.path.get_text()
      module = self.module.get_text()
      m = self.protocol.get_model()
      protocol = m.get_value(self.protocol.get_active_iter(), 0)
      self.window.destroy()
      return username, server, path, module, protocol
    else:
      self.window.destroy()
      raise RuntimeError
